import 'package:enterprise_v6_app/app/components/common/custom_text_widget.dart';
import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/closure/closure_appointment_entity.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/task_closure/task_waiting_management/task_waiting_management_controller.dart';

/// 我的任务-候检管理页面
class TaskWaitingManagementPage extends StatelessWidget {
  TaskWaitingManagementPage({super.key});

  final controller = Get.put(TaskWaitingManagementController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(titleText: '候检管理'),
            body: Container(
              color: const Color(0xffF5F5F6),
              padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.w),
              child: CustomEasyRefreshList<ClosureAppointmentEntity>(
                searchHint: '请输入车牌号搜索',
                key: controller.globalKey,
                isShowSearch: true,
                dataLoad: (pageIndex, pageSize, {String? search}) async {
                  return controller.getListData(pageIndex, pageSize, search: search);
                },
                itemBuilder: (ClosureAppointmentEntity item, int index) {
                  return CustomTitleChartPage(
                    title: item.licensePlate,
                    titleRightWidget: _getStateView(item.carStatus),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        // CustomTextWidget.getTextRowView(title: '车辆类型：', value: item.carTypeValue),
                        // AppGaps.vGap12,
                        CustomTextWidget.getTextRowView(title: '驾驶员姓名：', value: item.name),
                        AppGaps.vGap12,
                        CustomTextWidget.getTextRowView(title: '预计来访时间：', value: item.startDate),
                        AppGaps.vGap12,
                        _getApprovalButton()
                      ],
                    ),
                  ).clickOnce(() {
                    controller.toApprovalDetailsPage(item);
                  });
                },
              ),
            ),
          );
        });
  }

  /// 获取状态图标
  Widget _getStateView(String state) {
    String stateText = '';
    Color? color;
    switch (state) {
      case '2':
        stateText = '空载';
        color = const Color(0xff5C6DFF);
        break;
      case '1':
        stateText = '重载';
        color = const Color(0xffFC9900);
        break;
    }
    return Container(
      alignment: Alignment.center,
      height: 18.w,
      width: 52.w,
      decoration: BoxDecoration(
        color: color ?? Colors.white,
        borderRadius: BorderRadius.only(
          topRight: Radius.circular(20.w),
          topLeft: Radius.circular(20.w),
          bottomRight: Radius.circular(20.w),
        ),
      ),
      child: Text(
        stateText,
        style: TextStyle(
          fontSize: 10.sp,
          fontWeight: FontWeight.w400,
          color: Colors.white,
        ),
      ),
    );
  }

  /// 获取操作按钮
  Widget _getApprovalButton() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          '操作:',
          style: TextStyle(
            fontSize: 13.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: const Color(0xffA5A6B5),
          ),
        ),
        Container(
          padding: EdgeInsets.fromLTRB(10.w, 6.w, 6.w, 6.w),
          decoration: BoxDecoration(
            color: const Color(0xFFE5E7FF),
            borderRadius: BorderRadius.circular(4.w),
          ),
          child: Row(
            children: [
              Text(
                '检查',
                style: TextStyle(
                  fontSize: 13.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xff5C6DFF),
                ),
              ),
              AppGaps.hGap2,
              SvgPicture.asset(
                AppImages.icon_right_arrow,
              ),
            ],
          ),
        ),
      ],
    );
  }
}
